package com.liu.fresh.controller;


import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liu.fresh.biz.IAdminInfoBiz;
import com.liu.fresh.entity.AdminInfo;

@Controller
@RequestMapping("/admin")
public class AdminInfoController  {
	@Autowired
	private IAdminInfoBiz adminInfoBiz;

	@RequestMapping("/checkLogin")
	@ResponseBody
	public Object checkLogin(HttpSession session) {
		Object obj = session.getAttribute("currentLoginUser");
		if(obj == null){
			return "{\"code\":\"101\"}";
		} else {
			return obj;
		}
	}
	@RequestMapping("/login")
	@ResponseBody
	public int login(String aname, String pwd, HttpSession session) {			
		AdminInfo af = adminInfoBiz.login(aname, pwd);
		int result = 0;
		if(af != null){
			session.setAttribute("currentLoginUser", af);
			result = 1;
		}
		return result;
	}
	
	@RequestMapping("/success")
	public String loginSuccess(HttpSession session) {			
		if(session.getAttribute("currentLoginUser") != null){
			return "/WEB-INF/back/page/index.html";
		} else {
			return "/bk/index.html";//以/开头从项目目录开始算
		}
	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	public List<AdminInfo> findAll() {			
		return adminInfoBiz.findAll();
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public int add(String aname, String pwd, String tel) {			
		return adminInfoBiz.add(aname,pwd,tel);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public int update(String aid,String tel) {			
		return adminInfoBiz.update(aid,tel);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public int del(String aid) {			
		return adminInfoBiz.del(aid);
	}
	
	/*@RequestMapping("/upload")
	@ResponseBody
	public Map<String, String> upload(MultipartFile pics, HttpServletRequest request, @RequestParam Map<String, Object> params) {
		if (pics.isEmpty()){
			return Collections.emptyMap();
		}
		String savePath = "../pics";
		try{
			String path = request.getServletContext().getRealPath("");
			String temp = request.getServletContext().getInitParameter("uploadpath");
			if(!StringUtil.checkNull(temp)){
				savePath = temp;
			}
			
			savePath += "/" + new Date().getTime() + "_" + new Random().nextInt(10000) + "-" + pics.getOriginalFilename();
			
			File dest = new File(path, savePath);
			//将图片存到服务器的指定文件
			pics.transferTo(dest);
		} catch (IllegalStateException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		params.put("photo", savePath);
		
		if(adminInfoBiz.updatephoto(params) > 0){
			Map<String, String> map = new HashMap<String, String>();
			map.put("savepath", savePath);
			return map;
		} else {
			return Collections.emptyMap();
		}
		
	}*/
}
